<template>
    <div>
        <div class="search">
            <el-input placeholder="请输入内容" v-model="input" clearable size="mini" style="width: 300px">
            </el-input>
            <el-button type="primary" size="mini" @click="searchfun"><i class="el-icon-search"></i>查找</el-button>
            <el-button type="primary" size="mini" @click="addfun"><i class="el-icon-plus"></i>添加</el-button>
            <el-button type="primary" size="mini" @click="exportToExcel"><i class="el-icon-upload2"></i>导出</el-button>
        </div>
        <div class="table">
            <el-table :data="tableData" style="width: 99%" border :cell-style="{ fontSize: '12px' }">
                <el-table-column label="团购规则ID" width="160" prop="id" align="center">
                </el-table-column>
                <el-table-column label="商品ID" width="160" prop="goodsId" align="center"></el-table-column>
                <el-table-column label="名称" width="160" prop="goodsName" align="center"></el-table-column>
                <el-table-column label="图片" width="160" prop="picUrl" align="center">
                    <template slot-scope="scope">
                        <img :src="scope.row.picUrl" alt="" style="width: 80px" height="40px" />
                    </template>
                </el-table-column>
                <el-table-column label="团购优惠" width="140" prop="discount" align="center"></el-table-column>
                <el-table-column label="团购要求" width="140" prop="discountMember" align="center"></el-table-column>
                <el-table-column label="状态" width="160" prop="status" align="center">
                    <template slot-scope="scope">
                        <el-tag>{{
                            scope.row.status === 0 ? "正在进行" : "到期下线"
                            }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="结束时间" width="160" prop="expireTime" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="solt">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 20, 30, 40, 50, 100]" :page-size="size"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <div class="add">
            <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
                <el-form :model="form">
                    <el-form-item label="商品ID" :label-width="formLabelWidth">
                        <el-input v-model="form.goodsId" autocomplete="off" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="团购折扣" :label-width="formLabelWidth">
                        <el-input v-model="form.discount" autocomplete="off" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="团购人数要求" :label-width="formLabelWidth">
                        <el-input v-model="form.discountMember" autocomplete="off" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item label="过期时间" :label-width="formLabelWidth">
                        <el-date-picker v-model="form.expireTime" type="datetime" placeholder="选择日期时间" size="mini"
                            value-format="yyyy-MM-dd HH:mm:ss">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="qvedingfun">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import * as XLSX from "xlsx";
import { guizegetdata, guizeadd, guizeupdata, guizedelete } from "@/api/tggl/tggz";
export default {
    data() {
        return {
            // 添加表单
            falg: 0,
            dialogFormVisible: false,
            form: {
                discount: "",
                discountMember: "",
                expireTime: "",
                goodsId: "",
            },
            formLabelWidth: "120px",
            // 输入框
            input: "",
            // 分页
            size: 20,
            total: null,
            page: 1,
            // 表格
            tableData: [],
        };
    },
    created() {
        this.getdata();
    },
    methods: {
        // 点击确定
        qvedingfun() {
            console.log(this.form);
            if (this.falg === 1) {
                console.log(1111);
                guizeadd(this.form).then((res) => {
                    console.log(res);
                    if (res.errno == 0) {
                        this.dialogFormVisible = false;
                        this.getdata();
                        this.$message.success("添加成功");
                    } else {
                        this.dialogFormVisible = false;
                        this.$message.error(res.errmsg);
                    }
                });
            } else if (this.falg === 2) {
                guizeupdata(this.form).then((res) => {
                    if (res.errno == 0) {
                        this.dialogFormVisible = false;
                        this.getdata();
                        this.$message.success("保存成功");
                    } else {
                        this.dialogFormVisible = false;
                        this.$message.error(res.errmsg);
                    }
                });
            }
        },
        // 添加
        addfun() {
            this.dialogFormVisible = true;
            this.falg = 1;
            this.form = {
                discount: "",
                discountMember: "",
                expireTime: "",
                goodsId: "",
            };
        },
        // 导出
        exportToExcel() {
            console.log("导出");
            //  自定义表头
            const headers = [
                "团购规则ID",
                "商品ID",
                "名称",
                "图片",
                "团购优惠",
                "团购要求",
                "状态",
                "结束时间",
            ];
            // 自定义数据
            const data = this.tableData.map((item) => [
                item.id,
                item.goodsId,
                item.goodsName,
                item.picUrl,
                item.discount,
                item.discountMember,
                item.status,
                item.expireTime,
            ]);
            // 将表头和数据合并
            const worksheetData = [headers, ...data];
            // 将表头转换为工作表
            const worksheet = XLSX.utils.aoa_to_sheet(worksheetData);
            // 创建工作薄并导出
            const workbook = XLSX.utils.book_new();
            XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
            XLSX.writeFile(workbook, "团购规则.xlsx");
        },
        // 查找
        searchfun() {
            this.getdata();
        },
        // 获取初始数据
        getdata() {
            guizegetdata({
                page: this.page,
                limit: this.size,
                sort: "add_time",
                order: "desc",
                goodsId: this.input,
            })
                .then((res) => {
                    console.log(res);
                    if (res.errno == 0) {
                        this.tableData = res.data.list;
                        this.total = res.data.total;
                    }
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 表格操作
        handleEdit(index, row) {
            console.log(index, row);
            this.dialogFormVisible = true;
            this.falg = 2;
            this.form = row;
        },
        handleDelete(index, row) {
            console.log(index, row);
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        guizedelete(row).then((res) => {
                            console.log(res);
                            if (res.errno === 0) {
                                this.getdata();
                                this.$message.success('删除成功');
                            } else {
                                this.$message.error(res.errmsg);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        // 分页
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
        },
    },
};
</script>

<style scoped>
.search {
    margin-left: 20px;
    margin-top: 20px;
}

.table {
    margin-left: 20px;
    margin-top: 20px;
}

.solt {
    margin-top: 20px;
    margin-left: 20px;
}
</style>
